import 'dart:typed_data';

/// CreateTime 2020/4/24 9:51
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
class FileUtils{

  /// 临时目录: /data/user/0/com.example.myapp/cache
  /// 一个临时目录(缓存)，系统可以随时清除。
  static Future<String> getTempDir() async {
    try {
      Directory tempDir = await getTemporaryDirectory();
      return tempDir.path;
    } catch (err) {
      globalPrint(err);
      return null;
    }
  }

  /// 文档目录: /data/user/0/com.example.myapp/app_flutter
  /// 应用程序的目录，用于存储只有它可以访问的文件。只有当应用程序被删除时，系统才会清除目录。
  static Future<String> getAppDocDir() async {
    try {
      Directory appDocDir = await getApplicationDocumentsDirectory();
      return appDocDir.path;
    } catch (err) {
      globalPrint(err);
      return null;
    }
  }

  static readFile(filePath) {
    return new File('$filePath');
  }

  /// 读取json文件
  static Future<String> readJsonFile(filePath) async {
    try {
      final file = readFile(filePath);
      return await file.readAsString();
    } catch (err) {
      globalPrint(err);
      return null;
    }
  }

  /// 写入json文件
  static Future<File> writeJsonFile(obj, filePath) async {
    try {
      final file = readFile(filePath);
      return await file.writeAsString(json.encode(obj));
    } catch (err) {
      globalPrint(err);
      return null;
    }
  }

  ///文件重命名 File file = await FileUtils.renameFile('D://testFile//2.jpg', 'D://testFile//2.jpg');
  static Future<File> renameFile(String path,String newPath)async{
    File file = File(path);
    File newFile = file.renameSync(newPath);
//    file.deleteSync();
    return newFile;
  }
  ///文件移动 File file = await FileUtils.moveTo('D://testFile//2.jpg', 'D://测试//3.jpg');
  static Future<File> moveTo(String filePath ,String newPath)async{
    File file = File(filePath);
    File newFile = await file.copy(newPath);
    file.deleteSync();
    return newFile;
  }

  ///删除文件  FileSystemEntity fileSystemEntity = await FileUtils.deleteFile('D://测试//3.jpg');
  static Future<FileSystemEntity> deleteFile(String filePath)async{
    return File(filePath).delete(recursive: false);
  }

  ///创建文件 File file = await FileUtils.createFile('D://测试//3.jpg');
  static Future<File> createFile(String path){
    return File(path).create();
  }

  ///获取文件后缀   String prefix = FileUtils.getFilePrefix('D://测试//3.jpg');
  static String getFilePrefix(String path){
    return path.substring(path.lastIndexOf('.')+1,path.length);
  }

  ///获取文件名字       String fileName = FileUtils.getFileName('D:\\测试\\3.jpg');
  static String getFileName(String path){
    return path.substring(path.lastIndexOf(Platform.pathSeparator)+1,path.lastIndexOf('.'));
  }

  ///是否是文件夹
  static Future<bool> isDirector(String myPath){
    return FileSystemEntity.isDirectory(myPath);
  }

  ///是否是文件
  static Future<bool> isFile(String myPath){
    return FileSystemEntity.isFile(myPath);
  }

  ///保存图片 saveFile(savePath, res.bodyBytes);
  static Future<File> saveFile(String savePath, Uint8List unit8List){
    return File(savePath).writeAsBytes(unit8List);
  }
  ///下载图片    await FileUtils.downloadFileByUrl(pictureUrl, 'D:\\测试\\5.jpg');
  static Future<File> downloadFileByUrl(String url,String savePath)async{
    http.Response res = await http.get(url);
    return saveFile(savePath, res.bodyBytes);
  }
}